package com.yloveg.controller;

import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yloveg.base.BaseController;
import com.yloveg.common.PageUtil;
import com.yloveg.entity.VGift;
import com.yloveg.entity.VPayOrder;
import com.yloveg.service.VGiftService;
import com.yloveg.service.VPayOrderService;
import com.yloveg.service.VPayService;


@Controller
@RequestMapping({"gift"})  
public class GiftController extends BaseController {  
	
	@Autowired
    private VGiftService vGiftService;
	
	@Autowired
    private VPayOrderService vPayOrderService;

    @RequestMapping("/giftList")
    public String roleList(ModelMap map){
    	 return "landing/gift/giftList";
    }
    
    @RequestMapping("/queryGiftPage")
    @ResponseBody
    public Map<String, Object> queryGiftPage(int page, int limit){
        Map<String, Object> map = PageUtil.dateToLayTable(vGiftService.queryGiftPage(page, limit));
        return map;
    }
    
    @RequestMapping("/addGift")
    public String addGift(ModelMap map,int id){
    	VGift vgift=vGiftService.selectByPrimaryKey(id);
    	if(vgift==null) {
    		map.put("gift", new VGift());
    	}else {
    		map.put("gift", vgift);
    	}
    	return "landing/gift/addGift";
    }    

	@RequestMapping("/saveGift")
	@ResponseBody
	public Map<String, Object> saveGift(VGift vgift){
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
    	if(vgift.getId()==0) {
    		result = vGiftService.insertGift(vgift);
    	}else {
    		result = vGiftService.updateByPrimaryKeySelective(vgift);
    	}
		if(result>0){
			map.put("code", "1");
			map.put("msg", "操作成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
		}
		return map;		
	}

	@RequestMapping("/deleteGift")
	@ResponseBody
	public Map<String, Object> deleteGift (VGift vgift) {
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
		if(vgift.getId()!=null) {
			vgift.setSts("N");
			result = vGiftService.updateByPrimaryKeySelective(vgift);
		}
		if(result>0){
			map.put("code", "1");
			map.put("msg", "操作成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
		}
		return map;
	}
	
	@RequestMapping("/giftOrder")
    public String giftOrder(ModelMap map){
    	 return "landing/gift/giftOrder";
    }
	
	@RequestMapping("/queryGiftOrdePage")
    @ResponseBody
    public Map<String, Object> queryGiftOrdePage(int page, int limit,VPayOrder record){
        Map<String, Object> map = PageUtil.dateToLayTable(vPayOrderService.queryOrdersPage(page, limit, record));
        return map;
    }
}
